package cn.hl.hm.util.picRZ;


import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSONArray;

import net.sf.json.JSONObject;

/**
 * Created by luokaiwen on 17/5/23.
 */
public class IdCard {

    // data:image/jpeg;base64,
    private static String base64_image_string = "haha";

    
    /**
     *  
      * @Title: getIdNumByPic  
      * @Description: 提取身份证照片信息
      * @date: 2017年5月23日
      * @author:大和
      * @param picUrl
      * @return String 
      * @throws
     */
    public static String getIdNumByPic(String picUrl){
        String res = "";
    	
    	String imageToBase64 = Base64Utils.imageToBase64(picUrl);
    	if(StringUtils.isBlank(imageToBase64)){
    		System.out.println("...null，不请求阿里云");
    		return null;
    	}
    	
        String host = "https://dm-51.data.aliyun.com";
        String path = "/rest/160601/ocr/ocr_idcard.json";
        String method = "POST";
        String appcode = "f4e4a2c35a174ef4acb9592ff807f1c6";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        //根据API的要求，定义相对应的Content-Type
        headers.put("Content-Type", "application/json; charset=UTF-8");
        Map<String, String> querys = new HashMap<String, String>();
        String bodys = "{\n" +
                "  \"inputs\": [\n" +
                "    {\n" +
                "      \"image\": {\n" +
                "        \"dataType\": 50,\n" +
                "        \"dataValue\": \"" + imageToBase64 + "\"\n" +
                "      },\n" +
                "      \"configure\": {\n" +
                "        \"dataType\": 50,\n" +
                "        \"dataValue\": \"{\\\"side\\\":\\\"face\\\"}\"\n" +
                "      }\n" +
                "    }\n" +
                "  ]\n" +
                "}";

        try {
            /**
             * 重要提示如下:
             * HttpUtils请从
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
             * 下载
             *
             * 相应的依赖请参照
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
             */
            HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
            System.out.println(response.toString());
            //获取response的body
            String string = EntityUtils.toString(response.getEntity());
			System.out.println(string);
            
            JSONObject fromObject = JSONObject.fromObject(string);
			if(fromObject!=null){
				Object object = fromObject.get("outputs");
				if(object!=null){
					 JSONArray parseArray = JSONArray.parseArray(object.toString());
					 if(parseArray!=null){
						 Object object2 = parseArray.get(0);
						 if(object2!=null){
							 JSONObject fromObject2 = JSONObject.fromObject(object2);
							 if(fromObject2!=null){
								 Object object3 = fromObject2.get("outputValue");
								 if(object3!=null){
									 JSONObject fromObject3 = JSONObject.fromObject(object3);
									 if(fromObject3!=null){
										 Object object4 = fromObject3.get("dataValue");
										 if(object4!=null){
											 JSONObject fromObject4 = JSONObject.fromObject(object4);
											 if(fromObject4!=null){
												 res =  (String)fromObject4.get("num");
											 }
										 }
									 }
								 }
							 }
						 }
					 }
				}
			}
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    
		return res;
    	
    }
    public static void main(String[] args) {
    	String idNumByPic = getIdNumByPic("C:\\2017\\worktest\\apache-tomcat-7.0.73\\webapps\\1.jp");
    	System.out.println(idNumByPic);
    	/*
        String host = "https://dm-51.data.aliyun.com";
        String path = "/rest/160601/ocr/ocr_idcard.json";
        String method = "POST";
        String appcode = "f4e4a2c35a174ef4acb9592ff807f1c6";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        //根据API的要求，定义相对应的Content-Type
        headers.put("Content-Type", "application/json; charset=UTF-8");
        Map<String, String> querys = new HashMap<String, String>();
        String bodys = "{\n" +
                "  \"inputs\": [\n" +
                "    {\n" +
                "      \"image\": {\n" +
                "        \"dataType\": 50,\n" +
                "        \"dataValue\": \"" + base64_image_string + "\"\n" +
                "      },\n" +
                "      \"configure\": {\n" +
                "        \"dataType\": 50,\n" +
                "        \"dataValue\": \"{\\\"side\\\":\\\"face\\\"}\"\n" +
                "      }\n" +
                "    }\n" +
                "  ]\n" +
                "}";

        try {
            *//**
             * 重要提示如下:
             * HttpUtils请从
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
             * 下载
             *
             * 相应的依赖请参照
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
             *//*
            HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
            System.out.println(response.toString());
            //获取response的body
            String string = EntityUtils.toString(response.getEntity());
			System.out.println(string);
            JSONObject fromObject = JSONObject.fromObject(string);
         			if(fromObject!=null){
         				IdRoots bean = (IdRoots)JSONObject.toBean(fromObject, IdRoots.class);
         				List<Outputs> outputs2 = bean.getOutputs();
         				if(outputs2!=null&&outputs2.size()>0){
         					Outputs outputs = outputs2.get(0);
         					String dataValue = outputs.getOutputValue().getDataValue();
         					
         					
         					
         					System.out.println(dataValue);
         				}
         				
         			}
        } catch (Exception e) {
            e.printStackTrace();
        }
    */}
}
